#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/*
    pclose等待新进程的结束，而不是杀新进程。
*/
int main()
{
    FILE *read_fp;
    char buffer[BUFSIZ + 1];
    int chars_read;
    memset(buffer, '\0', sizeof(buffer));

    read_fp = popen("ps -ax", "r");
    if (read_fp != NULL)
    {
        // 读取被调用进程的输出
        chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);

        while (chars_read > 0) 
        {
            buffer[chars_read - 1] = '\0';
            printf("Reading:-\n %s\n", buffer);
            chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
        }

        pclose(read_fp);
        exit(EXIT_SUCCESS);
    }
    exit(EXIT_FAILURE);
}